//
//  JiuViewController.m
//  简单豆瓣不得不交版 1 带
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "JiuViewController.h"
#import "FillJiuCollectionViewCell.h"
#import "DetialViewController.h"
#import "FilmModel.h"

static NSString *const celljiu = @"CELL";

@interface JiuViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *myArray;


@end

@implementation JiuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myArray = [[NSMutableArray alloc] init];
    self.view.backgroundColor = [UIColor orangeColor];
    // Do any additional setup after loading the view.
    
    [self handleGata];
    
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake(120, 180);
    //九宫格
    
    
    UICollectionView *view1 = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowlayout];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    //签协议
    
    view1.delegate  =self;
    view1.dataSource = self;
    
    //必须使用协议方法
     [view1 registerClass:[FillJiuCollectionViewCell class] forCellWithReuseIdentifier:celljiu];
    //添加滑动方法
    flowlayout.scrollDirection =UICollectionViewScrollDirectionVertical;
    //设置  距离上下左右的高度
    flowlayout.sectionInset = UIEdgeInsetsMake(20, 10, 30, 10);
    view1.tag = 100001;
    

    
    
}


//方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.myArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FillJiuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:celljiu forIndexPath:indexPath];
    
    FilmModel *model = [self.myArray objectAtIndex:indexPath.row];
    cell.model = model;
   
    return cell;
}


- (void)handleGata {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@".txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *HXArray = [dic objectForKey:@"result"];
    for (NSDictionary *dic in HXArray) {
        
        FilmModel *model = [[FilmModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.myArray addObject:model];
        
        
    }
    
    
}

//跳转到详情页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DetialViewController *DetialVC = [[DetialViewController alloc]init];
    NSDictionary *dic = [self.myArray objectAtIndex:indexPath.row];
   
    DetialVC.mytitle = [dic valueForKey:@"movieName"];

    
    [self.navigationController pushViewController:DetialVC animated:YES];
    
    
}

 



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
